package com.rainwood.material.uitls;

import android.content.Context;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextDirectionHeuristic;
import android.text.TextDirectionHeuristics;
import android.text.TextPaint;
import android.text.TextUtils;

import androidx.core.content.ContextCompat;

import com.rainwood.material.App;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * @author hhx
 * @date 2021/3/4
 * @Description：
 */
public class ToolUtils {
    public static StaticLayout getStaticLayout(CharSequence label, Paint paint, int width, Layout.Alignment alignment, float lineHeight, TextUtils.TruncateAt truncate, int maxLines) {
        StaticLayout sl = null;
        TextPaint textPaint = new TextPaint(paint);
        Class clazz = null;
        try {
            clazz = Class.forName("android.text.StaticLayout");
        } catch (ClassNotFoundException e1) {
            e1.printStackTrace();
        }
        Constructor<?>[] cons = null;
        cons = clazz.getConstructors(); //第一步，取得全部构造方法并赋给数组
        Constructor con = null;
        StaticLayout tmp = null;
        try {
            //**13个参数 ，注意int.class 不是interge.class**
            con = clazz.getConstructor(CharSequence.class, int.class, int.class, TextPaint.class, int.class,
                    Layout.Alignment.class, TextDirectionHeuristic.class, float.class, float.class, boolean.class,
                    TextUtils.TruncateAt.class, int.class, int.class);
        } catch (NoSuchMethodException e1) {
            e1.printStackTrace();
        }

        if (con == null) {
            return null;
        }
        try {
            //+Math.round(textPaint.getTextSize()*3/2)
            tmp = (StaticLayout) con.newInstance(label, 0, label.length(), textPaint, width, alignment, TextDirectionHeuristics.FIRSTSTRONG_LTR, lineHeight, 0, false, truncate, width, maxLines);
            sl = tmp;
        } catch (InstantiationException e1) {
            e1.printStackTrace();
        } catch (IllegalAccessException e1) {
            e1.printStackTrace();
        } catch (InvocationTargetException e1) {
            e1.printStackTrace();
        }
        return sl;
    }


    public static int float2Int(float f) {
        return Math.round(f);
    }

    public static long double2Int(double f) {
        return Math.round(f);
    }

    public static int string2Int(String str) {
        int ret = 0;
        if (str == null || "".equalsIgnoreCase(str) == true) {
            return ret;
        }
        try {
            ret = Integer.parseInt(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ret;
    }

    /**
     * dp2px
     *
     * @param context
     * @param dpValue
     * @return
     */
    public static int dip2px(Context context, float dpValue) {

        return Math.round(ScreenAdapter.dp2px(Math.round(dpValue)));
    }

    public static int dip2px(float dpValue) {
        Context context = App.getInstance().getApplicationContext();
        return ToolUtils.dip2px(context, dpValue);
    }


    public static float sp2px(Context context, float sp) {
        float scale = context.getResources().getDisplayMetrics().scaledDensity;
        return sp * scale;
    }

    public static float sp2px(float sp) {
        Context context = App.getInstance().getApplicationContext();
        return sp2px(context, sp);
    }


    /**
     * 获取资源
     *
     * @param mContent
     * @param res
     * @return
     */
    public static Drawable getDrawable(Context mContent, int res) {
        return ContextCompat.getDrawable(mContent, res);
    }

    /**
     * 获取资源文本
     *
     * @param context
     * @param resId
     * @return
     */
    public static String getResourcesString(Context context, int resId) {
        if (context == null) {
            context = App.getInstance();
        }
        return context.getString(resId);
    }

    /**
     * 获取资源文本
     *
     * @param context
     * @param resId
     * @return
     */
    public static String getResourcesString(Context context, int resId, Object... formatArgs) {
        if (context == null) {
            context = App.getInstance().getBaseContext();
        }
        return context.getString(resId, formatArgs);
    }
}
